#define _GNU_SOURCE
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "tlpi.h"


static void
handler(int sig)
{
	printf("PID %ld: caught signal %2d (%s)\n", (long)getpid(), 
			sig, strsignal(sig));
}

int
main(int argc, char *argv[])
{
	pid_t parentPid, childPid;
	int j;
	struct sigaction sa;

	if(argc < 2)
		fatal("usage error\n");

	setbuf(stdout, NULL);

	parentPid = getpid();
	printf("PID of parent process is:	%ld\n", (long)parentPid);
	printf("Foreground process group ID is: %ld\n", (long)tcgetpgrp(STDIN_FILENO));

	for(j=1; j<argc; j++)
	{
		childPid = fork();

		if(childPid == -1)
			errExit("fork() failed\n");

		if(childPid == 0)
		{
			if(argv[j][0] == 'd')
				if(setpgid(0, 0) == -1)
					errExit("setpgid() failed");

			sa.sa_handler = handler;
			sa.sa_flags = 0;
			sigemptyset(&sa.sa_mask);
			if(sigaction(SIGHUP, &sa, NULL) == -1)
				errExit("sigaction() failed");
			break;
		}
	}

	alarm(60);
	
	printf("PID=%ld, PGID=%ld\n", (long)getpid(), (long)getpgrp());

	for(;;)
		pause();
}
